package com.hxh.controller;

import com.hxh.entity.ProjectDatasource;
import com.hxh.entity.output.ApiResult;
import com.hxh.entity.po.TableInfo;
import com.hxh.service.ProjectDatasourceService;
import com.hxh.service.ProjectService;
import com.hxh.service.ProjectTablePageConfigService;
import com.hxh.service.TableConfigService;
import com.hxh.util.MysqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: AICode
 * @Package: com.example.controller
 * @ClassName: TableConfigController
 * @Author: HXH
 * @Description: 项目表配置
 * @Date: 2023/12/11 9:32
 */
@RestController
@RequestMapping("/api/table")
@Slf4j
public class TableConfigController {
    @Autowired
    ProjectService projectService;

    @Autowired
    ProjectDatasourceService projectDatasourceService;

    @Autowired
    TableConfigService tableConfigService;

    @Autowired
    ProjectTablePageConfigService projectTablePageConfigService;

    @RequestMapping(value = "/listAll", produces = "application/json;charset=utf-8")
    public ApiResult listAll(@RequestParam Integer datasourceId) {
        ProjectDatasource projectDatasource = projectDatasourceService.selectById(datasourceId);
        if (projectDatasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        String dataBaseName = projectDatasource.getMysqlDatabaseName();
        try {
            List<TableInfo> tableInfos = tableConfigService.getAllTableByDataBaseName(projectDatasource, dataBaseName);
            return ApiResult.ok("获取项目所有表信息成功", tableInfos);
        } catch (Exception e) {
            log.error("获取项目所有表信息失败！", e);
            return ApiResult.fail("获取项目所有表信息失败！");
        }
    }

    @RequestMapping(value = "/listAllByDatasourceId", produces = "application/json;charset=utf-8")
    public ApiResult listAllByDatasourceId(Integer datasourceId) {
        if (datasourceId == null){
               return ApiResult.ok("获取项目所有表信息成功", new ArrayList<>());
        }
        ProjectDatasource projectDatasource = projectDatasourceService.selectById(datasourceId);
        if (projectDatasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        String dataBaseName = projectDatasource.getMysqlDatabaseName();
        try {
            List<TableInfo> tableInfos = tableConfigService.getAllTableByDataBaseName(projectDatasource, dataBaseName);
            return ApiResult.ok("获取项目所有表信息成功", tableInfos);
        } catch (Exception e) {
            log.error("获取项目所有表信息失败！", e);
            return ApiResult.fail("获取项目所有表信息失败！");
        }
    }

    @RequestMapping(value = "/update", produces = "application/json;charset=utf-8")
    public ApiResult update(@RequestParam Integer datasourceId, @RequestParam String sql, @RequestParam String tableName) {
        ProjectDatasource datasource = projectDatasourceService.selectById(datasourceId);
        if (datasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        try {
            MysqlUtils.executeOneSqlWithException(datasource, sql);
            return ApiResult.ok("更新表成功");
        } catch (Exception e) {
            log.error("更新表失败！", e);
            return ApiResult.fail("更新表失败！");
        }
    }

    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    public ApiResult delete(@RequestParam Integer datasourceId, @RequestParam String tableName) {
        ProjectDatasource datasource = projectDatasourceService.selectById(datasourceId);
        if (datasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        try {
            tableConfigService.deleteTable(datasource, tableName);
            return ApiResult.ok("删除表成功");
        } catch (Exception e) {
            log.error("删除表失败！", e);
            return ApiResult.fail("删除表失败！");
        }
    }

    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    public ApiResult add(@RequestParam Integer datasourceId, @RequestParam String addSql, @RequestParam String tableName) {
        ProjectDatasource datasource = projectDatasourceService.selectById(datasourceId);
        if (datasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        try {
            MysqlUtils.executeOneSqlWithException(datasource, addSql);
            return ApiResult.ok("新增表成功");
        } catch (Exception e) {
            log.error("新增表失败！", e);
            return ApiResult.fail("新增表失败！");
        }
    }

    @RequestMapping(value = "/editTableInfo", produces = "application/json;charset=utf-8")
    public ApiResult editTableInfo(@RequestParam Integer datasourceId, @RequestParam String tableName,
                                   String tableDesc, @RequestParam String oldTableName) {
        ProjectDatasource datasource = projectDatasourceService.selectById(datasourceId);
        if (datasource == null) {
            return ApiResult.fail("数据源不存在！");
        }
        try {
            if (!oldTableName.equals(tableName)) {
                String sql = "RENAME TABLE " + oldTableName + " TO " + tableName + ";";
                MysqlUtils.executeOneSqlWithException(datasource, sql);
            }
            if (StringUtils.isNotBlank(tableDesc)) {
                String sql = "ALTER TABLE " + tableName + " COMMENT '" + tableDesc + "';";
                MysqlUtils.executeOneSqlWithException(datasource, sql);
            }
            return ApiResult.ok("更新表成功");
        } catch (Exception e) {
            log.error("更新表失败！", e);
            return ApiResult.fail("更新表失败！");
        }
    }
}
